package com.xinchen.aiowaste.vm

import android.text.TextUtils
import androidx.lifecycle.MutableLiveData
import com.blankj.utilcode.util.GsonUtils
import com.maple.model.data.AioUserInfo
import com.maple.model.entity.aio.LabelInfoEntity
import com.maple.model.entity.aio.WarehousEntity
import com.maple.model.entity.aio.WarehouseListEntity
import com.maple.utils.store.AioDBHelper
import com.xinchen.aiowaste.base.BaseViewModel
import com.xinchen.aiowaste.model.repository.CommonRepository
import java.util.WeakHashMap

class PrintLabelViewModel: BaseViewModel(){


    private val repository by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED) { CommonRepository() }

    val userInfo: AioUserInfo? by lazy {
        AioDBHelper.getUser()
    }

    val entId: String by lazy {
        userInfo?.entId?:""
    }

    val userId: String by lazy {
        userInfo?.userId?:""
    }

    val labelInfoData: MutableLiveData<LabelInfoEntity.Data> = MutableLiveData()

    fun getLabelInfo(code: String) {
        val params: WeakHashMap<String, Any> = WeakHashMap()
        params.put("entId", entId)
        params.put("qrcode", code)

        launchOnlyResult(
            block = {
                repository.labelInfo(params)
            }, success = {
                val entity = GsonUtils.fromJson(GsonUtils.toJson(it), LabelInfoEntity::class.java)
                entity.data?.let { data ->
                    labelInfoData.postValue(data)
                }
            })
    }
}